package com.yogee.topseller.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.yogee.core.utils.ToastUtils;
import com.yogee.topseller.R;

/**
 * Created by Administrator on 2018/3/14.
 */

public class MPopWindow extends PopupWindow {
    private View conentView;
    private Activity activity;
    public MPopWindow(final Activity context, final String phone) {
        activity = context;
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        conentView = inflater.inflate(R.layout.pop_call, null);
//        int h = context.getWindowManager().getDefaultDisplay().getHeight();
//        int w = context.getWindowManager().getDefaultDisplay().getWidth();
        this.setContentView(conentView);
        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        // 刷新状态
        this.update();
        // 实例化一个ColorDrawable颜色为半透明66000000
        ColorDrawable dw = new ColorDrawable(66000000);
        // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
        this.setBackgroundDrawable(new ColorDrawable(0x00000000));
        this.setOnDismissListener(new PopupWindow.OnDismissListener() {
            //在dismiss中恢复透明度
            public void onDismiss() {
                WindowManager.LayoutParams lp = context.getWindow().getAttributes();
                lp.alpha = 1f;
                context.getWindow().setAttributes(lp);
            }
        });

        ImageView img_close_btn = (ImageView) conentView.findViewById(R.id.img_close_btn);
        img_close_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
        TextView tv_phone = (TextView) conentView.findViewById(R.id.tv_phone);
        tv_phone.setText(phone);
        TextView create_shansou_btn = (TextView) conentView.findViewById(R.id.create_shansou_btn);
        create_shansou_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!phone.equals("")) {
                    try{

                        Intent intent = new Intent(Intent.ACTION_DIAL);
                        Uri data = Uri.parse("tel:" + phone);
                        intent.setData(data);
                        context.startActivity(intent);
                        dismiss();
                    }catch(Exception e){
                        ToastUtils.showToast(context,"您的手机暂时不支持此功能");
                    }

                }else{
                    ToastUtils.showToast(context,"该产品暂时不支持此功能");
                }
            }
        });
    }

    /**
     * 显示popupWindow
     *
     * @param parent
     */
    public void showPopupWindow(View parent) {
        if (!this.isShowing()) {
            WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
            lp.alpha = 0.5f;
            activity.getWindow().setAttributes(lp);
            // 以下拉方式显示popupwindow
            this.showAtLocation(parent, Gravity.CENTER, -20, 0);
        } else {
            this.dismiss();
        }
    }

}
